package org.hxd.notice.controller;

import org.hxd.notice.service.NoticeService;
import org.hxd.pojo.Notice;
import org.hxd.pojo.User_inf;
import org.hxd.units.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class noticeController {
    @Autowired
    NoticeService noticeService;

    //查找公告信息
    @RequestMapping("/findnotice")
    public String findnotice(Model model,Notice notice,@RequestParam(defaultValue = "1") int pageIndex){
        //查找总记录数
        int recordCount = noticeService.findNoticeCount(notice);
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        pageModel.setRocordCount(recordCount);
        model.addAttribute("pageModel", pageModel);
        //分页查询
        List<Notice> notices = noticeService.findnotice(notice,pageModel);
        model.addAttribute("notices",notices);
        model.addAttribute("notice",notice);
        return "jsp/notice/notice";
    }

    //添加公告
    @RequestMapping("/addNotice")
    @ResponseBody
    public Object addNotice(Notice notice, HttpSession session){
        User_inf user = (User_inf) session.getAttribute("user");
        int rows = noticeService.addNotice(notice,user);
        if(rows > 0){
            int noticeCount = noticeService.findNoticeCount(null);
            PageModel pageModel = new PageModel();
            pageModel.setRocordCount(noticeCount);
            return pageModel.getTotalSize();
        }else {
            return "FAIl";
        }
    }

    //删除公告信息
    @RequestMapping("/removeNotice")
    @ResponseBody
    public String removeNotice(Integer[] ids){
        int rows = noticeService.removeNotice(ids);
        if(rows == ids.length){
            return "OK";
        }else {
            return "FAIL";
        }
    }

    //根据id查找公告信息，进入修改页面
    @RequestMapping("/findNoticeById")
    public String findNoticeById(int id,Model model,int pageIndex,String title,String content){
        Notice notice = noticeService.findNoticeById(id);
        model.addAttribute("notice",notice);
        model.addAttribute("pageIndex",pageIndex);
        model.addAttribute("content",content);
        model.addAttribute("title",title);
        return "jsp/notice/showUpdateNotice";
    }

    //修改公告
    @RequestMapping("/updateNotice")
    @ResponseBody
    public String updateNotice(Notice notice){
        int rows = noticeService.updateNotice(notice);
        if(rows > 0){
            return "OK";
        }else {
            return "FAIL";
        }
    }

    //预览公告
    @RequestMapping("/previewNotice")
    public String previewNotice(Integer id,Model model){
        Notice notice = noticeService.findNoticeById(id);
        model.addAttribute("notice",notice);
        return "jsp/notice/previewNotice";
    }
}
